import request from '@/utils/request'
import {ApiResponse,PageData,PageQuery,BaseDetail,TreeOption} from "@/ts/CommonTypes"

import {downloadExcel} from "@/api/ApiUtils";

const urlPrefix = "/api/authOrganization/";

export class Organization{
    id: number = null;
    key: string = null;
    name: string = null;
    parentId: number = null;
    sort: number = null;
    title: string = null;
    uniquePath: string = null;
    children:Organization[] = [];
    constructor(init?: Partial<Organization>) {
        if (init) {
            Object.assign(this, init);
        }
    }
}


export class QueryData{
    total: number = 0;
    size: number = 100;
    current: number = 1;
    constructor(init?: Partial<QueryData>) {
        if (init) {
            Object.assign(this, init);
        }
    }
}

/**
 * 查询详情
 * @param id
 */
export function getById(id:number) {
    return request<ApiResponse<Organization>>({
        url:  urlPrefix + 'getById?id='+ id ,
        method: 'GET'
    })
}

export function getTree() {
    return request<ApiResponse<Organization[]>>({
        url:  urlPrefix + 'getTree' ,
        method: 'GET'
    })
}

export function getTreeSelector(exclusionId = null) {
    return request<ApiResponse<TreeOption>>({
        url:  urlPrefix +  (exclusionId ? 'getTreeSelector?exclusionId=' + exclusionId : "getTreeSelector"),
        method: 'GET'
    })
}


/**
 * 查询列表
 * @param query
 */
export function queryList(query:QueryData) {
    return request<ApiResponse<PageData<Organization>>>({
        url:  urlPrefix + 'page',
        method: 'POST',
        data:query
    })
}

/**
 * 导出列表
 * @param query
 */
export function exportList(query:QueryData) {
    return request.post(
        urlPrefix + 'export',
        query,
        {responseType:"blob"}
    ).then((res)=>{
        downloadExcel(res,"组织列表.xlsx");
    })
}


/**
 * 保存数据
 * @param type
 */
export function save (data:Organization) {
    return request({
        url: urlPrefix + 'save',
        method: 'POST',
        data: data
    })
}

/**
 * 删除数据
 * @param note
 */
export function deleteById (id:number) {
    if(!id || id <= 0){
        return null;
    }
    return request({
        url: urlPrefix + 'deleteById?id='+ id ,
        method: 'POST'
    })
}
